<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 16:49:33
 * @version 1.0
 */

class TixianAction extends AdminAction {

    public function index(){
    	$userid  = I('get.userid');
    	$status  = I('get.status');

    	if ($status!="") {
    		$map['status']=$status;
    	}

		if ($userid != '') {
			$map['memberID'] = $userid;
		}

		$obj = M('Tixian');
		$count = $obj->where($map)->count();
		import("@.ORG.Page");
		$page = new Page($count, 10);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->assign('userid', $userid);
		$this->display();
    }

    #通过
	public function pass() {
		$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('参数错误');
		}
		$map['id']=$id;
		$map['status']=0;
		$obj = M('Tixian');
		$list = $obj->where($map)->find();
		if (!$list) {
			$this->error('信息不存在');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}

	#拒绝
	public function refuse() {
		$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('参数错误');
		}
		$map['id']=$id;
		$map['status']=0;
		$obj = M('Tixian');
		$list = $obj->where($map)->find();
		if (!$list) {
			$this->error('信息不存在');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}

	#更新
	public function Update() {
		$obj = D('Tixian');		
		if ($data = $obj->create()) {
			if ($data['status']==2) {
				if ($data['remark']=='') {
					$state = '请输入拒绝理由';
					echo $this->echo_json_str($state,$message,$url);
					die;
				}
			}

			$map['status']=0;
			$map['id']=$data['id'];
			if ($obj->where($map)->save($data)){	

				//返回佣金
				if ($data['status']==2) {
					$list = $obj->field('memberID,money')->where('id='.$data['id'])->find();
					$where['id']=$list['memberID'];
					M('Member')->where($where)->setInc('money',$list['money']);
				}

				$state = 'SUCCESS';
	            $message = '操作成功';
	            $url = U('Tixian/index');
			} else {
				$state = '操作失败';
			}			
		} else {
			$state = $obj->getError();
		}
		echo $this->echo_json_str($state,$message,$url);
	}


	#拒绝
	public function view() {
		$id = (int) $_GET['id'];
		if (!isset ($id)) {
			$this->error('参数错误');
		}
		$map['id']=$id;
		$map['status']=array("neq",0);
		$obj = M('Tixian');
		$list = $obj->where($map)->find();
		if (!$list) {
			$this->error('信息不存在');
		} else {
			$this->assign('list', $list);
			$this->display();
		}
	}
	

}